package org.handover.config.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices;

import javax.sql.DataSource;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //@Autowired
    //private LoginService userDetailsService;

    @Autowired
    @Qualifier("loginService")
    private UserDetailsService userDetailsService;

    @Autowired
    @Qualifier("dataSource")
    private DataSource dataSource;

    /*
        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) {
            auth.authenticationProvider(customAuthenticationProvider);

            // 在这里将 UserDetailsSercie 实例注入
            try {
                auth.userDetailsService(userDetailsService);
            } catch (Exception e) {
                //logger.error("Set userDetailService failed, {}", e.getMessage());
                e.printStackTrace();
            }
        }
    */
    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .authorizeRequests()
                .antMatchers("/admin/userInformation/**").authenticated()
                //.antMatchers("/admin/**").fullyAuthenticated()
                .antMatchers("/admin/userGroup/**").hasAnyRole("root","admin")
                .antMatchers("/admin/user/**").hasAnyRole("root","admin")
                .antMatchers("/error","/css/**","/js/**","/fonts/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login").permitAll()
                .and()
                .logout().permitAll()
                .and()
                .rememberMe()
                .rememberMeServices(rememberMeServices())
                .tokenValiditySeconds(60480)
                .key("secret")
                ;
    }

    @Bean
    public RememberMeServices rememberMeServices() {
        JdbcTokenRepositoryImpl rememberMeTokenRepository = new JdbcTokenRepositoryImpl();
        rememberMeTokenRepository.setDataSource(dataSource);

        // 这里也注入了 UserDetailsSerice 实例
        PersistentTokenBasedRememberMeServices rememberMeServices =
                new PersistentTokenBasedRememberMeServices("secret", userDetailsService, rememberMeTokenRepository);

        //rememberMeServices.setParameter(REMEMBER_ME);
        return rememberMeServices;
    }
}
